package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plz.framework.cache.CacheService;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.SysParamsDto;
import com.plz.afmt.dto.SysParamsQueryDto;
import com.plz.afmt.entity.SysParamsEntity;
import com.plz.afmt.mapper.SysParamsMapper;
import com.plz.afmt.service.SysParamsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.SysParamsTransfer;
import com.plz.afmt.vo.SysParamsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 参数管理
 *
 * @author jzc
 */
@Service
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParamsEntity> implements SysParamsService {
    @Autowired
    private SysParamsTransfer sysParamsTransfer;
    @Autowired
    private CacheService cacheService;

    @Override
    public PagerResult<SysParamsVo> pageQuery(SysParamsQueryDto dto) {
        QueryWrapper<SysParamsEntity> queryWrapper = PageCommonUtil.dtoToWrapper(SysParamsEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<SysParamsEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<SysParamsVo> voPagerResult = sysParamsTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public SysParamsEntity addOrUpdate(SysParamsDto dto) {
        SysParamsEntity entity = new SysParamsEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<SysParamsEntity> addOrUpdateList(List<SysParamsDto> dto) {
        List<SysParamsEntity> entitys = sysParamsTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<SysParamsVo> listFor(SysParamsQueryDto dto) {
        QueryWrapper<SysParamsEntity> queryWrapper = PageCommonUtil.dtoToWrapper(SysParamsEntity.class, dto);
        List<SysParamsEntity> list = this.list(queryWrapper);
        List<SysParamsVo> vos = sysParamsTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public SysParamsVo detail(Long id) {
        SysParamsEntity entity = this.getById(id);
        SysParamsVo vo = new SysParamsVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public String getValue(String paramCode) {
        return this.getOne(new LambdaQueryWrapper<SysParamsEntity>()
                .eq(SysParamsEntity::getParamCode, paramCode)).getParamValue();
    }

}



